package top.itmeans.yes.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.itmeans.yes.dto.*;
import top.itmeans.yes.entity.Order;
import top.itmeans.yes.entity.OrderOperateHistory;
import top.itmeans.yes.mapper.OrderMapper;
import top.itmeans.yes.service.OrderOperateHistoryService;
import top.itmeans.yes.service.OrderService;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author Yiming.Cao
 * @since 2022-04-28
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Resource
    private OrderMapper orderMapper;
    @Autowired
    private OrderOperateHistoryService orderOperateHistoryService;


    @Override
    public Page<Order> list(OrderQueryParam queryParam, Integer pageSize, Integer pageNum) {
        Page<Order> page = new Page<>(pageNum,pageSize);
        //page.setRecords(orderMapper.getList(queryParam));
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!StrUtil.isEmpty(queryParam.getOrderSn()), Order::getOrderSn,queryParam.getOrderSn())
                .eq(ObjectUtil.isNotNull(queryParam.getStatus()), Order::getStatus,queryParam.getStatus())
                .eq(ObjectUtil.isNotNull(queryParam.getSourceType()), Order::getSourceType,queryParam.getSourceType())
                .eq(ObjectUtil.isNotNull(queryParam.getOrderType()), Order::getOrderType,queryParam.getOrderType())
                .like(!StrUtil.isEmpty(queryParam.getCreateTime()), Order::getCreateTime,queryParam.getCreateTime())
                .like(!StrUtil.isEmpty(queryParam.getReceiverKeyword()), Order::getReceiverName,queryParam.getReceiverKeyword())
                .or()
                .like(!StrUtil.isEmpty(queryParam.getReceiverKeyword()), Order::getReceiverName,queryParam.getReceiverKeyword());
        return orderMapper.selectPage(page,queryWrapper);
    }

    @Override
    public int delivery(List<OrderDeliveryParam> deliveryParamList) {
        int count = orderMapper.delivery(deliveryParamList);
        //添加操作记录
        List<OrderOperateHistory> operateHistoryList = deliveryParamList.stream()
                .map(omsOrderDeliveryParam -> {
                    OrderOperateHistory history = new OrderOperateHistory();
                    history.setOrderId(omsOrderDeliveryParam.getOrderId());
                    history.setCreateTime(LocalDateTime.now());
                    history.setOperateMan("后台管理员");
                    history.setOrderStatus(2);
                    history.setNote("完成发货");
                    return history;
                }).collect(Collectors.toList());
        orderOperateHistoryService.saveBatch(operateHistoryList);
        return count;
    }

    @Override
    @Transactional(rollbackFor = top.itmeans.yes.common.exception.ApiException.class)
    public boolean close(List<Long> ids, String note) {
        List<Order> orderList = ids.stream().map(orderId -> {
            Order order = new Order();
            order.setId(orderId);
            order.setStatus(4);
            return order;
        }).collect(Collectors.toList());
        boolean orderFlag = this.updateBatchById(orderList);
        List<OrderOperateHistory> operateHistoryList = ids.stream().map(orderId -> {
            OrderOperateHistory history = new OrderOperateHistory();
            history.setOrderId(orderId);
            history.setCreateTime(LocalDateTime.now());
            history.setOperateMan("后台管理员");
            history.setOrderStatus(4);
            history.setNote("订单关闭:"+note);
            return history;
        }).collect(Collectors.toList());
        boolean orderOperateHistoryFlag = orderOperateHistoryService.saveBatch(operateHistoryList);
        return orderFlag&&orderOperateHistoryFlag;
    }

    @Override
    @Transactional(rollbackFor = top.itmeans.yes.common.exception.ApiException.class)
    public int delete(List<Long> ids) {
        return orderMapper.deleteBatchIds(ids);
    }

    @Override
    public OrderDetail detail(Long id) {
        return orderMapper.getDetail(id);
    }

    @Override
    public int receiverInfoParam(ReceiverInfoParam receiverInfoParam) {
        Order order = new Order();
        order.setId(receiverInfoParam.getOrderId());
        order.setReceiverName(receiverInfoParam.getReceiverName());
        order.setReceiverPhone(receiverInfoParam.getReceiverPhone());
        order.setReceiverPostCode(receiverInfoParam.getReceiverPostCode());
        order.setReceiverDetailAddress(receiverInfoParam.getReceiverDetailAddress());
        order.setReceiverProvince(receiverInfoParam.getReceiverProvince());
        order.setReceiverCity(receiverInfoParam.getReceiverCity());
        order.setReceiverRegion(receiverInfoParam.getReceiverRegion());
        order.setModifyTime(LocalDateTime.now());
        int count = orderMapper.updateById(order);
        //插入操作记录
        OrderOperateHistory history = new OrderOperateHistory();
        history.setOrderId(receiverInfoParam.getOrderId());
        history.setCreateTime(LocalDateTime.now());
        history.setOperateMan("后台管理员");
        history.setOrderStatus(receiverInfoParam.getStatus());
        history.setNote("修改收货人信息");
        orderOperateHistoryService.save(history);
        return count;
    }

    @Override
    public int updateMoneyInfo(MoneyInfoParam moneyInfoParam) {
        Order order = new Order();
        order.setId(moneyInfoParam.getOrderId());
        order.setFreightAmount(moneyInfoParam.getFreightAmount());
        order.setModifyTime(LocalDateTime.now());
        int count = orderMapper.updateById(order);
        //插入操作记录
        OrderOperateHistory history = new OrderOperateHistory();
        history.setOrderId(moneyInfoParam.getOrderId());
        history.setCreateTime(LocalDateTime.now());
        history.setOperateMan("后台管理员");
        history.setOrderStatus(moneyInfoParam.getStatus());
        history.setNote("修改费用信息");
        orderOperateHistoryService.save(history);
        return count;
    }

    @Override
    public int updateNote(Long id, String note, Integer status) {
        Order order = new Order();
        order.setId(id);
        order.setNote(note);
        order.setModifyTime(LocalDateTime.now());
        int count = orderMapper.updateById(order);
        OrderOperateHistory history = new OrderOperateHistory();
        history.setOrderId(id);
        history.setCreateTime(LocalDateTime.now());
        history.setOperateMan("后台管理员");
        history.setOrderStatus(status);
        history.setNote("修改备注信息："+note);
        orderOperateHistoryService.save(history);
        return count;
    }
}
